#include "gui.h"



g_graphics::g_graphics()
{
	col = new string[ parameters::get_instance()->game_parameters[ "MAX_PL" ] ];

	p = new QPixmap( "pictures/p.gif" );

	fire = new QPen( QBrush( QColor( 255, 0, 0 ) ), 2, Qt::DotLine );

	map_background = new QBrush( QColor( 100, 100, 100 ), Qt::SolidPattern );


	col[0] = "Blue";
	col[1] = "Red";

	terrain_pixmaps[ 0 ] = new QPixmap( "pictures/t0.gif" );
	terrain_pixmaps[ 10 ] = new QPixmap( "pictures/t10.gif" );
	terrain_pixmaps[ 20 ] = new QPixmap( "pictures/t20.gif" );
	terrain_pixmaps[ 30 ] = new QPixmap( "pictures/t30.gif" );
	terrain_pixmaps[ 40 ] = new QPixmap( "pictures/t40.gif" );
	terrain_pixmaps[ 50 ] = new QPixmap( "pictures/t50.gif" );
	terrain_pixmaps[ 60 ] = new QPixmap( "pictures/t60.gif" );
	terrain_pixmaps[ 70 ] = new QPixmap( "pictures/t70.gif" );
	terrain_pixmaps[ 80 ] = new QPixmap( "pictures/t80.gif" );
	terrain_pixmaps[ 90 ] = new QPixmap( "pictures/t90.gif" );
	terrain_pixmaps[ 100 ] = new QPixmap( "pictures/t100.gif" );
	terrain_pixmaps[ 20000 ] = new QPixmap( "pictures/t20000.gif" );
	terrain_pixmaps[ 30000 ] = new QPixmap( "pictures/t30000.gif" );

	begin_pixmaps[ "Blue" ] = new QPixmap( "pictures/p-1.gif" );
	begin_pixmaps[ "Red" ] = new QPixmap( "pictures/p-2.gif" );

	bonus_pixmaps[ "First aid kit" ] = new QPixmap( "pictures/bfak.gif" );
};



g_graphics::~g_graphics()
{
	delete p;

	delete fire;

	delete map_background;

	for ( std::map< std::string, std::map< std::string, std::pair< QPixmap*, QPixmap* > > >::iterator it = units_pixmaps.begin(); it != units_pixmaps.end(); ++it )
	{
		for ( std::map< std::string, std::pair< QPixmap*, QPixmap* > >::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2 )
		{
			delete it2->second.first;
			delete it2->second.second;
		}
	}

	for ( std::map< unsigned int, QPixmap* >::iterator it = terrain_pixmaps.begin(); it != terrain_pixmaps.end(); ++it )
	{
		delete it->second;
	}

	for ( std::map< std::string, QPixmap* >::iterator it = bonus_pixmaps.begin(); it != bonus_pixmaps.end(); ++it )
	{
		delete it->second;
	}

	for ( std::map< std::string, std::map< std::string, QSound* > >::iterator it = units_sounds.begin(); it != units_sounds.end(); ++it )
	{
		for ( std::map< std::string, QSound* >::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2 )
		{
			delete it2->second;
		}
	}

	for ( std::map< std::string, QPixmap* >::iterator it = begin_pixmaps.begin(); it != begin_pixmaps.end(); ++it )
	{
		delete it->second;
	}
};



std::string g_graphics::get_color( unsigned int x )
{
	return col[ x ];
};



void g_graphics::set_new_units_resources( string name, std::vector< std::pair< string, string > >& icons, string sound_f )
{
	string picpref = "pictures/";
	string sndpref = "sounds/";

	for ( unsigned int i = 0; i < parameters::get_instance()->game_parameters[ "MAX_PL" ]; ++i )
	{
		string pn_name = picpref + icons[ i ].first;
		string ps_name = picpref + icons[ i ].second;
		QPixmap* pn = new QPixmap( pn_name.c_str() );
		QPixmap* ps = new QPixmap( ps_name.c_str() );
		units_pixmaps[ name ][ get_color( i ) ] = std::make_pair< QPixmap*, QPixmap* >( pn, ps );
	};

	string sf_name = sndpref + sound_f;
	units_sounds[ name ][ "Fire" ] = new QSound( sf_name.c_str() );
};






void MyQScrollArea::setSizeHint( QSize s )
{
	unsigned int w = s.width();
	unsigned int h = s.height();
	if( w >= 800 )
		w = 800;
	if( h >= 600 )
		h = 600;
	size = QSize( w, h );
};



QSize MyQScrollArea::sizeHint() const
{
	return size;
};



gui::gui( QWidget *parent, Qt::WFlags flags ): QMainWindow( parent, flags )
{
	QMenuBar* menu_bar = new QMenuBar( this );
	menu_bar->addAction( "neco" );
	setMenuBar( menu_bar );
	QScrollArea* sa = new QScrollArea( this );
	begin_w = new begin_widget( sa );
	sa->setWidget( begin_w );
	sa->setWidgetResizable( true );
	this->setCentralWidget( sa );
};



gui::~gui()
{

};



game_gui* game_gui::get_instance()
{
	static game_gui inst( 0 );
	return &inst;
};



void game_gui::get_graphics()
{
	graphics = new g_graphics();
};



void game_gui::create_gui()
{
	g = new gui();
};



void game_gui::show_game_end( player* won_p )
{
	std::string message;
	message = won_p->get_name() + " won";
	QMessageBox::information( 0, "Game end", message.c_str() );
	qApp->quit();
};



void game_gui::show_move(unit* u, unsigned int x, unsigned int y, pre_move_info* pmi )
{
	g->play_w->show_move( u, x, y, pmi );
};



void game_gui::show_fire(unit* u, unsigned int x, unsigned int y, pre_fire_info* pfi)
{
	g->play_w->show_fire( u, x, y, pfi );
};



void game_gui::show_next_turn()
{
	g->play_w->show_next_turn();
};


void game_gui::set_paused( bool paused )
{
	g->play_w->set_paused( paused );
};



pre_move_info::pre_move_info( unit* u, unsigned int x, unsigned int y )
{
	ox = u->get_x();
	oy = u->get_y();
};



pre_fire_info::pre_fire_info( unit* u, unsigned int x, unsigned int y )
{
	s = ( ( game_gui::get_instance()->g->play_w->selected_unit != 0 ) && ( game_gui::get_instance()->g->play_w->selected_unit->u == ( dynamic_cast< unit* >( mapa::get_instance()->get_object( x, y ) ) ) ) );
};